package com.deng.theone.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.deng.theone.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {

    @NacosInjected
    private NamingService namingService;
    TestService service;

    private TestController(TestService service) {
        this.service = service;
    }

    @NacosValue(value = "${key1:valN}", autoRefreshed = true)
    private String key1;

    /**
     * 获取nacos动态配置
     *
     * @return nacos动态配置
     */
    @GetMapping("/test")
    public String test() {
        return "test:" + key1;
    }

    /**
     * 构造器注入参数
     *
     * @return 结果集
     */
    @GetMapping("/get")
    public List get() {
        return service.getList();
    }

    @GetMapping("/getInstance")
    public List<Instance> getInstance   (@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }

}
